package com.hiedu.controller;


import com.hiedu.beans.dto.ProductAddDTO;
import com.hiedu.beans.dto.ProductUpdateDTO;
import com.hiedu.beans.entity.Product;
import com.hiedu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Controller
@RestController //返回数据不是界面
@RequestMapping("/api/product")//一级
public class ProductController {

    //调用业务方法
    //依赖注入
    @Autowired
    private ProductService productService;


    @GetMapping("list")//二级
    public Object getProductList(){
        return productService.getProductList();
    }

    @GetMapping("{id}")
    public Object getProductById(@PathVariable Integer id){
        return productService.getProductById(id);}

    @DeleteMapping("{id}")
    public Object deleteProductById(@PathVariable Integer  id){
        productService.deleteProductById(id);
        return "删除成功";
    }

    @PostMapping("add")
    //@RequestBody 把前端传过来的json数据转成java对象
    public Object add(@Validated @RequestBody ProductAddDTO  productAddDTO){
        productService.add(productAddDTO);
        return "添加成功";
    }

    //修改
    @PutMapping("update")
    public Object update(@RequestBody ProductUpdateDTO   productUpdateDTO) {
        productService.update(productUpdateDTO);
        return "修改商品成功";
    }

}
